package org.fuys.coder.infrastructure.repository;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.fuys.coder.common.constants.ResultMessageConstants;
import org.fuys.coder.common.exception.BusinessException;
import org.fuys.coder.domain.substance.model.vo.SubstanceFileTypeVO;
import org.fuys.coder.domain.substance.repository.ISubstanceFileRepository;
import org.fuys.coder.infrastructure.dao.SubstanceFileMapper;
import org.fuys.coder.infrastructure.model.po.SubstanceFile;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.infrastructure.repository
 * @className: SubstanceFileRepositoryImpl
 * @author: WangJing
 * @description: 仓储实现类
 * @date: 2024/7/14 23:51
 * @version: 1.0
 */
@Service
public class SubstanceFileRepositoryImpl extends ServiceImpl<SubstanceFileMapper, SubstanceFile>
        implements ISubstanceFileRepository {

    @Resource
    private SubstanceFileMapper substanceFileMapper;

    @Override
    public void batchInsert(Long substanceId, Map<Integer,Long> fileMap) {
        if(ObjectUtils.isEmpty(fileMap)){
            return;
        }else if(fileMap.isEmpty()){
            return;
        }
        List<SubstanceFile> fileList=new ArrayList<>();
        fileMap.forEach((k,v)->{
            SubstanceFile substanceFile = new SubstanceFile();
            substanceFile.setFileId(v);
            //判断是否是合法的文件类型
            if(!SubstanceFileTypeVO.contains(k)){
                throw new BusinessException(ResultMessageConstants.ILLEGAL_OPERATION+":文件类型不合法:"+k);
            }
            substanceFile.setFileType(k);
            substanceFile.setSubstanceId(substanceId);
            fileList.add(substanceFile);
        });
        this.saveBatch(fileList);
    }
}
